<?php

namespace app\command;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Db;

class GzhAction extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('gzhaction')
            ->setDescription('the gzhaction command');
    }
    protected function execute(Input $input, Output $output)
    {
        $this->wx_send();

    }
    public function send_url($url,$jsonData)
    {
        // 请求函数 参数($url,$jsonData)
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        // https 支持 - 对认证证书来源的检查
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        // 请求数据放这里
        curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
        // head
        // curl_setopt($ch, CURLOPT_HTTPHEADER, $head);

        $response = curl_exec($ch);

        return $response;

    }
    public function http_curl($url,$type,$res,$arr)
    {
        /**
         * $url 请求的url
         * $type 请求类型
         * $res 返回数据类型
         * $arr post请求参数
         */
        $ch=curl_init();

        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        if($type=='post'){
            curl_setopt($ch,CURLOPT_POST,1);
            curl_setopt($ch,CURLOPT_POSTFIELDS,$arr);
        }
        $output = curl_exec($ch);
        curl_close($ch);
        if($res=='json'){
            return json_decode($output,true);
        }

    }

    public function wx_send()
    {

        // mini_program_fans

        //wx_gzh_fans

        $gzh_openids = Db::name('mini_program_fans')->alias('m')

            ->join('wx_gzh_fans g', 'g.unionId = m.unionId', 'LEFT')

            ->where([['g.openid','>','0']])

            ->column('g.openid');

        // echo "<pre>";
        // print_r($gzh_openids);
        // echo "</pre>";


//        $gzh_openids = ['orXbIs5Il9x5cC9wT_kP-GzS29Wk','orXbIs9RDp16tEveSqcWIYB82R6c'];
//        print_r($gzh_openids);exit;

        // exit;

        $num = 0;
        foreach ($gzh_openids as $key => $value) {
            $this->wx_send_cs($value,0);
            $num++;
            print_r($num.PHP_EOL);
        }



        // $openid = 'orXbIs9RDp16tEveSqcWIYB82R6c';
        // $model->wx_send_cs($openid,0);

        // echo 'over';

        // $customer_ids = Db::name('customer')->where('shop_id','79209378385066')->column('id');

        // echo "<pre>";
        // print_r($customer_ids);
        // echo "</pre>";
        // // exit;

        // $start_time = strtotime('2023-12-01 00:00:00');
        // $end_time = strtotime('2023-12-31 23:59:59');
        // $tag_shop_id = '73782774880422';
        // foreach ($customer_ids as $key => $value) {
        //     // 修改客户归属门店
        //     Db::name('customer')->where('id','=',$value)->update(['shop_id'=>$tag_shop_id]);

        //     // 修改客户预存门店归属
        //     Db::name('customer_balance')->where('customer_id','=',$value)->update(['shop_id'=>$tag_shop_id]);

        //     // 修改客户卡项
        //     Db::name('service')->where('customer_id','=',$value)->update(['shop_id'=>$tag_shop_id]);

        //     // 修改客户核销记录
        //     Db::name('service_log')->where([['customer_id','=',$value],['create_time','between',"$start_time,$end_time"]])->update(['shop_id'=>$tag_shop_id]);
        // }
    }
    public function wx_send_cs($openid,$status)
    {
        // echo "模板消息20231219";
        // exit;


        $url = "https://api.weixin.qq.com/cgi-bin/token";

        // 公众号
        $jsonData = [

            'grant_type' => 'client_credential',

            'appid'      => 'wx7cde54d2bb9607e6',

            'secret'     => '1aa86183eca8d6ebf854cbc0349543df',

        ];

        $data = $this->send_url($url,$jsonData);


        $res = json_decode($data,true);

        // echo "<pre>";
        // print_r($res);
        // echo "</pre>";
        // exit;

        $token = $res['access_token'];

        $posturl = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$token;

        $dt = array(
            "touser" => $openid,
            'template_id'  => 'xl3Z_wkofGfGtgad2UkIKLktqyh8jYqNjToHvNTYetM',
            'url'   => "",
            'miniprogram' => array(
                'appid' => 'wxfee2f64c9d301f2f',
                'pagepath' => 'pages/activity20231212/activity20231212?status='.$status,
            ),
            'data' => array(
                'first' => array('value' => '欣奕'),
                'keyword1' =>array('value' => '欣奕用户'),
                'keyword2' =>array('value' => date('Y-m-d H:i:s')),
                'remark' =>array('value' => '点击此处评价此次客服质量。'),
            ),
        );


        $postjson = json_encode($dt);

        $resder = $this->http_curl($posturl,'post','json',$postjson);

        // var_dump($resder);

    }
}
